/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.RuntimeData;
import core.ScreenEngine;
import network.Connection;
import network.PacketBuilder;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class SendMessageForm extends BasicForm
{
    private static final int SAND_COMMAND_ID = 1;
    
    private CheckBox cbToDisp;
    private TextField toCallSign;
    private TextField messageText;
    
    private Label toCallSignLabel;
    private Label messageTextLabel;
            
    public SendMessageForm()
    {
        super("send_message");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);
        
        cbToDisp = new CheckBox("to_disp");
        cbToDisp.setSelectCommandText("");
        cbToDisp.setSelected(true);
        cbToDisp.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae) 
            {            
                setFormContents();
            }
        });  
        
        toCallSignLabel = new Label("to_callsign");
        toCallSign = new TextField(); 
        toCallSign.setInputModeOrder(new String[]{"123", "ABC","abc"});
        toCallSign.setSelectCommandText("");
        toCallSign.setConstraint(TextArea.ANY);
        
        messageTextLabel = new Label("message_text");
        messageText = new TextField(); 
        messageText.setInputModeOrder(new String[]{"АБВ", "абв", "123", "ABC", "abc"});
        messageText.setSelectCommandText("");
        messageText.setConstraint(TextArea.ANY);
        
        //Команды
        addCommand(new BackCommand());
        
        Command sandCommand = new Command("sand", SAND_COMMAND_ID) 
        {
            public void actionPerformed(ActionEvent evt)
            {                
                sandMessage();
                        
                ScreenEngine.getEngine().popTo(0);
                
                evt.consume();
            }
        };
        addCommand(sandCommand);    
        
        //Добавляем остальные поля
        setFormContents();
    }
    
    public void show()
    {
        //Убираем среднюю кнопку
        if (Display.getInstance().isTouchScreenDevice())
        {
            Display.getInstance().setThirdSoftButton(false); 
        }
        
        super.show();
    }
    
    public void hideNotify() 
    {
        //Возвращаем среднюю кнопку
        Display.getInstance().setThirdSoftButton(true); 
    }
    
    private void setFormContents()
    {
        //Чистим форму
        removeAll();
        
        //Заполняем форму нужными компонентами
        addComponent(cbToDisp);
        if (!cbToDisp.isSelected())
        {
            addComponent(toCallSignLabel);
            addComponent(toCallSign);
        }   
        addComponent(messageTextLabel);
        addComponent(messageText);
        
        setFocused(cbToDisp);
        
        repaint();
    }
    
    private void sandMessage()
    {
        boolean toDisp = cbToDisp.isSelected();
        if (messageText.getText().equals("") || (!toDisp && toCallSign.getText().equals("")))
        {
            return;
        }
        
        //Формируем и отправляем запрос
        RuntimeData runtimeData = IMAPMIDlet.getMidlet().getRuntimeData();
        byte[] body = PacketBuilder.createIMapMessage((toDisp ? PacketBuilder.TO_DISPETCHER : PacketBuilder.POINT_TO_POINT), 
                                                      (toDisp ? "" : toCallSign.getText()), messageText.getText(), runtimeData.getPeopleID());
        byte[] data = PacketBuilder.createSrvTransfereData(PacketBuilder.DEFAULT_CONNECTION_TYPE, runtimeData.getSrvID(), 
                                                           PacketBuilder.DEFAULT_DESTINATION_ID, runtimeData.getGUID(), true, body);
        Connection.getConnection().send(data);
    }
}
